package org.dairy.farms.nandana.web.controller;

import org.dairy.farms.model.*;
import org.dairy.farms.nandana.core.NandanaUtils;
import org.dairy.farms.nandana.core.logic.LivestockManagementLogic;
import org.dairy.farms.nandana.web.view.LactationView;
import org.dairy.farms.nandana.web.view.LivestockView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Divya
 * Date: Jul 4, 2011
 * Time: 5:06:23 PM
 */
@Controller
public class LivestockController {
    private static final Logger logger = LoggerFactory.getLogger(LivestockController.class);
    @Autowired
    private LivestockManagementLogic livestockManagementLogic;
    @Autowired
    private NandanaUtils nandanaUtils;

    @RequestMapping(value = "livestock/searchLivestock.json", method = RequestMethod.POST)
    public void searchLivestock(@RequestParam(value = "searchBy", required = true) String searchBy,
                                @RequestParam(value = "value", required = true) String searchParam,
                                @RequestParam(value = "page", required = true) int page,
                                @RequestParam(value = "start", required = true) int start,
                                @RequestParam(value = "limit", required = true) int limit,
                                Model model) {
        List<Livestock> livestockList = livestockManagementLogic.searchLivestockBy(searchBy, searchParam, page, start, limit);
        int totalCount = livestockManagementLogic.searchLivestockBy(searchBy, searchParam);
        model.addAttribute("livestockList", livestockList);
        model.addAttribute("totalCount", totalCount);
    }

    @RequestMapping(value = "livestock/getGenders", method = RequestMethod.GET)
    public void getGenders(Model model) {
    }

    @RequestMapping(value = "livestock/getLivestockStatusTypes", method = RequestMethod.GET)
    public void getLivestockStatusTypes(Model model) {
        List<LivestockStatusType> livestockStatusTypes = livestockManagementLogic.getLivestockStatusTypes();
        model.addAttribute("livestockStatusTypes", livestockStatusTypes);
    }

    @RequestMapping(value = "livestock/createLivestock", method = RequestMethod.POST)
    public void createLivestock(Model model, @RequestParam(value = "givenId", required = true) String givenId,
                                @RequestParam(value = "farmId", required = true) Long farmId,
                                @RequestParam(value = "name", required = true) String name,
                                @RequestParam(value = "motherId", required = false) Long motherId,
                                @RequestParam(value = "fatherId", required = false) Long fatherId,
                                @RequestParam(value = "genetics", required = true) String genetics,
                                @RequestParam(value = "height", required = true) Long height,
                                @RequestParam(value = "listPrice", required = true) float listPrice,
                                @RequestParam(value = "sellingPrice", required = true) float sellingPrice,
                                @RequestParam(value = "purchasePrice", required = true) float purchasePrice,
                                @RequestParam(value = "purchaseDate", required = true) String purchaseDate,
                                @RequestParam(value = "weight", required = true) Double weight,
                                @RequestParam(value = "gender", required = true) String gender,
                                @RequestParam(value = "dateOfBirth", required = true) String dateOfBirth,
                                @RequestParam(value = "statusType", required = true) Long livestockStatusTypeId,
                                @RequestParam(value = "note", required = false) String note) {

        LivestockView livestockView = new LivestockView();
        livestockView.setFarmId(farmId);
        livestockView.setGivenId(givenId);
        livestockView.setName(name);
        livestockView.setMotherId(motherId);
        livestockView.setFatherId(fatherId);
        livestockView.setGenetics(genetics);
        livestockView.setHeight(height);
        livestockView.setListPrice(listPrice);
        livestockView.setSellingPrice(sellingPrice);
        livestockView.setPurchasePrice(purchasePrice);
        livestockView.setPurchaseDate(nandanaUtils.getDate(purchaseDate, "M/d/yyyy"));
        livestockView.setWeight(weight);
        livestockView.setDateOfBirth(nandanaUtils.getDate(dateOfBirth, "M/d/yyyy"));
        livestockView.setGender(gender.equals(Livestock.Gender.MALE.name()) ? Livestock.Gender.MALE : gender.equals(Livestock.Gender.FEMALE.name()) ? Livestock.Gender.FEMALE : Livestock.Gender.UNKNOWN);
        livestockView.setLivestockStatusTypeId(livestockStatusTypeId);
        livestockView.setNotes(note);
        Livestock livestock = livestockManagementLogic.createLivestock(livestockView);
        model.addAttribute("livestock", livestock);
    }

    @RequestMapping(value = "livestock/getLivestockDetails", method = RequestMethod.POST)
    public void getLivestockDetails(Model model, @RequestParam(value = "id", required = true) Long[] livestockId) {
        List<Livestock> livestockList = livestockManagementLogic.getLivestockDetails(livestockId);
        model.addAttribute("livestockList", livestockList);
        model.addAttribute("total", livestockList.size());
    }

    @RequestMapping(value = "livestock/upload", method = RequestMethod.POST)
    public Model upload(HttpServletRequest request, Model model, HttpServletResponse response, @RequestParam("file") MultipartFile f) throws IOException {
        if (f == null) {
            return model.addAttribute("msg", "The file is null.");
        }
        String livestockId = request.getParameter("livestockId");
        LivestockFileType livestockFileType = new LivestockFileType();
        livestockFileType.setFileName(f.getOriginalFilename());
        livestockFileType.setContent(f.getBytes());
        livestockFileType.setSize(f.getSize());

        return model.addAttribute("msg", "File ( " + f.getOriginalFilename() + ") successfully uploaded.");
    }
}
